package com.realife.ysuper.controller;

import com.realife.common.utils.R;
import com.realife.ysuper.entity.BrandEntity;
import com.realife.ysuper.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * 品牌表，一个品牌下有多个商品（spu），一对多关系
 *
 * @author Albert
 * @email 15962463559@163.com
 * @date 2020-06-05 10:04:15
 */
@Slf4j
@RestController
@RequestMapping("/ysuper/brand")
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ysuper:brand:info")
    public R info(@PathVariable("id") Long id) {
        BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     *
     * @Valid @RequestBody BrandEntity brand, BindingResult result紧跟校验的结果
     * JSR303后端校验: javax.validation
     * @NotBlank(message = "品牌名必须提交")
     * private String name;
     * @URL(message = "logo必须是合法URL")
     * private String logo;
     * @Pattern(regexp="/^[a-zA-Z]$/", message="xxxxxxxxx")
     * private String firstLetter;
     * @Min(value=0,message="排序必须大于等于0") private Integer sort;
     * <p>
     * <p>
     * <p>
     * <p>
     * <p>
     * /ysuper/brand/save
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ysuper:brand:save")
    public R save(@Valid @RequestBody BrandEntity brand, BindingResult result) {

        if (result.hasErrors()) {
            Map<String, String> map = new HashMap<>();

            result.getFieldErrors().forEach((item) -> {
                //错误提示
                String message = item.getDefaultMessage();
                //哪个字段错了?
                String field = item.getField();
                map.put(field, message);
            });


            return R.error(400, "非法参数").put("error", map);
        } else {
            brandService.save(brand);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ysuper:brand:update")
    public R update(@RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ysuper:brand:delete")
    public R delete(@RequestBody Long[] ids) {
        brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * http://192.168.1.100:8082/ysuper/brand/brandListPageQuery/1/10
     *
     * @param currentPage
     * @param pageSize
     * @param map
     * @return
     */
    @RequestMapping("/brandListPageQuery/{currentPage}/{pageSize}")
    public R brandListPageQuery(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("pageSize") Integer pageSize,
            @RequestBody(required = false) Map map
    ) {

        System.out.println(currentPage);
        System.out.println(pageSize);
        System.out.println(map);

        return R.ok();
    }
}
